package com.hzhq.contract.convert.method;

import com.hzhq.contract.bean.dto.CompanyDTO;
import com.hzhq.contract.bean.entity.*;
import com.hzhq.contract.bean.vo.CompanyVO;
import com.hzhq.contract.repo.ContractTypeRepository;
import com.hzhq.contract.repo.DepartRepository;
import com.hzhq.contract.repo.UserRepository;
import com.hzhq.contract.status.DefaultValue;
import lombok.extern.slf4j.Slf4j;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.validation.groups.Default;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author hzhq
 * @create 2021/4/12 5:30 下午
 * @email hzhq1255@163.com
 */

@Slf4j
public class ContractTemplateConvertMethod {


    public static ContractType string2ContractType(String name) {
        return ContractType.builder().typeName(name).build();
    }

    public static User string2User(String name) {
        return User.builder().username(name).build();

    }

    public static Depart string2Depart(String name) {
        return Depart.builder().departName(name).build();
    }


    public static String companiesToString(List<Company> companies) {
        StringBuilder companiesString = new StringBuilder();
        if (companies == null) {
            return null;
        }
        for (Company s : companies) {
            if (s == null) {
                continue;
            }
            companiesString.append(s.getCompanyName()).append(",");
        }
        if (companiesString.length() == 0) {
            return "";
        }
        companiesString.delete(companiesString.lastIndexOf(","), companiesString.length());
        return companiesString.toString();
    }



    public static List<CompanyVO> entityList2VOList(List<Company> companyList) {
        companyList = companyList.stream().filter(e -> e.getDeleted() == 0).collect(Collectors.toList());
        List<CompanyVO> companyListString = new ArrayList<>();
        if (null != companyList) {
            companyListString = companyList.stream().map(e ->
                    CompanyVO.builder().companyId(e.getCompanyId().toString())
                            .companyName(e.getCompanyName())
                            .phone(e.getPhone())
                            .email(e.getEmail())
                            .type(e.getType())
                            .address(e.getAddress())
                            .build())
                    .collect(Collectors.toList());
        }

        return companyListString;
    }


    public static List<Company> voList2entityList(List<CompanyVO> companyVOList) {
        List<Company> companyList = new ArrayList<>();
        if (null != companyVOList) {
            companyList = companyVOList.stream().map(e -> Company.builder()
                    .companyId(new ObjectId(e.getCompanyId()))
                    .companyName(e.getCompanyName())
                    .type(e.getType())
                    .address(e.getAddress())
                    .phone(e.getPhone())
                    .email(e.getEmail())
                    .deleted(DefaultValue.NOT_DELETED)
                    .build()).collect(Collectors.toList());
        }
        return companyList;
    }


}
